package com.jiyun.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jiyun.model.sign.SigningInfo;
import com.jiyun.model.sign.dto.SigningInfoAndResidentsDto;
import com.jiyun.model.sign.dto.SigningInfoQuery;
import com.jiyun.result.Result;
import com.jiyun.service.ISigningInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【-SigningInfo】操作接口
 *
 * @author ???
 * @since 2024-09-27
 */
@RestController
@RequestMapping("/signingInfo")
@Api(tags = "操作接口")
@CrossOrigin
public class SigningInfoController {

    @Autowired
    public ISigningInfoService signingInfoService;

    /**
     * 接口：添加
     * @param signingInfo 要添加的
     * @return Result 响应给前端
     */
    @PostMapping
    @ApiOperation("添加" )
    public Result save(@RequestBody SigningInfo signingInfo) {
        signingInfoService.save(signingInfo);
        return Result.ok();
    }

    /**
     * 接口：根据Id删除
     * @param id 的Id
     * @return Result 响应给前端
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据Id删除")
    public Result remove(@PathVariable("id" ) Long id) {
        signingInfoService.removeById(id);
        return Result.ok();
    }

    /**
     * 接口：修改
     * @param signingInfo 要修改的数据
     * @return Result 响应给前端
     */
    @PutMapping("update")
    @ApiOperation("修改")
    public Result updateById(@RequestBody SigningInfo signingInfo) {
        signingInfoService.updateById(signingInfo);
        return Result.ok();
    }


    /**
      * 根据Id查询单个
     */
    @GetMapping("/get/{id}")
    @ApiOperation("根据Id查询单个")
    public Result getById(@PathVariable Long id)  {
        SigningInfo one = signingInfoService.getById(id);
        return Result.ok(one);
    }

    /**
     * 接口：查询所有
     */
    @GetMapping
    @ApiOperation("查询所有")
    public Result getList() {
        List<SigningInfo> listAll = signingInfoService.list();
        return Result.ok(listAll);
    }


    /**
     * 接口：分页查询所有
     * @return List<SigningInfo> 所有数据
     */
    @GetMapping("/{page}/{limit}")
    @ApiOperation("获取所有")
    public Result getPageList(@PathVariable Integer page, @PathVariable Integer limit, SigningInfoQuery query) {
        //封装分页参数
        query.setCurrentPage(page);
        query.setPageSize(limit);
        //查询分页数据
        IPage<SigningInfo> pageList = signingInfoService.getPageList(query);
        return Result.ok(pageList);
    }
    @GetMapping("/findByPendIdSigningInfo")
    @ApiOperation("根据pendingSigningId查询")
    public Result findByPendIdSigningInfo(Integer pendingSigningId) {
        SigningInfo signingInfo= signingInfoService.findByPendIdSigningInfo(pendingSigningId);
        if (signingInfo == null){
            return Result.fail();
        }
        return Result.ok(signingInfo);
    }
    @PostMapping("/updateSigningInfo")
    @ApiOperation("修改居民信息和签约信息")
    public Result updateSigningInfo(@RequestBody SigningInfoAndResidentsDto signingInfoAndResidentsDto) {
//        System.out.println( signingInfoAndResidentsDto.getSigningInfo());
        signingInfoService.updateSigningInfo(signingInfoAndResidentsDto);
        return Result.ok();
    }

}
